package com.dudu.service.impl;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.mortbay.log.Log;

import com.dudu.access.ImsVoiceNoticeUtils;
import com.dudu.bean.AccountBean;
import com.dudu.bean.AppExtendBean;
import com.dudu.bean.ChargingRuleBean;
import com.dudu.bean.MeetInfoBean;
import com.dudu.bean.PoolAbilityInfoBean;
import com.dudu.bean.ResponseSendVoiceBean;
import com.dudu.enumeration.AbilityTypeEnum;
import com.dudu.service.IMeetInfoService;
import com.dudu.service.IRuleService;
import com.dudu.service.IVoiceCaptchaService;
import com.dudu.timer.SendVoiceUtil;

import core.CoreDao;
import core.CoreDaoInvoke;

/**
 * 语音验证码实现类
 * 
 * @author 危东
 * @date 2016年5月17日
 * 
 */
public class VoiceCaptchaServiceImpl implements IVoiceCaptchaService {

    private static final Logger log = core.util.Logger.getLogger(VoiceCaptchaServiceImpl.class.getSimpleName());
    // 用于查询数据用
    private CoreDao coreDao = CoreDaoInvoke.getInstance();

    /**
     * 发起语音验证码
     * 
     * @param appExtendBean
     *            应用信息
     * @param accountBean
     *            账号信息
     * @param userIdentify
     *            用户标识
     * @param called
     *            被叫号码
     * @param content
     *            短信内容
     * @param usePlatNbr
     *            强制使用平台号
     * @return
     */
    @Override
    public JSONObject sendVoiceCaptcha(AppExtendBean appExtendBean, AccountBean accountBean, String userIdentify, String called, String content, String usePlatNbr) {
	log.info("VoiceCaptchaServiceImpl.sendVoiceCaptcha 发起语音验证码");
	// 最终返回结果
	JSONObject object = new JSONObject();
	// 处理上传的文字信息
	content = content.replaceAll("\\pP|\\pS", " ");
	// 查询当前账号默认使用的多方通话能力通道
	// 获取语音验证码能力类型标识
	String abilityType = AbilityTypeEnum.VCAPTCHA.getKey();
	// 能力配置查询
	IRuleService ruleService = new RuleServiceImpl();
	ChargingRuleBean chargingRule = ruleService.queryChargingRule(appExtendBean.getApp_key(), accountBean.getCust_account(), abilityType);
	// 判断查询的默认通道调用数据是否为空
	if (chargingRule != null) {
	    String displayNum = null;// 显号
	    String number_vest = null;// 地区
	    String pool_ability_id = null;// 显号ID
	    SendVoiceUtil sendUtil = new SendVoiceUtil();
	    PoolAbilityInfoBean poolAbilityInfo = sendUtil.queryPoolAbilityInfo(coreDao, appExtendBean.getApp_key(), accountBean.getCust_account(), chargingRule.getType(),
		    pool_ability_id, displayNum, usePlatNbr, number_vest);
	    if (poolAbilityInfo == null) {
		if (AbilityTypeEnum.RONGLIANAUDEO.getKey().equals(chargingRule.getType())) {
		    poolAbilityInfo = new PoolAbilityInfoBean();
		} else {
		    object.put("result", "-1");
		    object.put("errorMsg", "未配置对应显号;ability_type:" + chargingRule.getType());
		    object.put("describe", "未配置显号");
		    object.put("code", "0018");
		    return object;
		}
	    }

	    // 根据查询的账户默认调用的能力配置，调用相应的通道
	    // 判断如果是ims语音验证码信息，则调用ims语音验证码接口
	    if (AbilityTypeEnum.VCAPTCHA_IMS.getKey().equals(chargingRule.getType())) {
		// 进行相应业务处理
		object = sendVoiceCaptcha(appExtendBean, accountBean, userIdentify, called, poolAbilityInfo.getCharge_number(), poolAbilityInfo.getCharge_key(),
			poolAbilityInfo.getDisplay_number(), content, AbilityTypeEnum.VCAPTCHA_IMS.getKey());
	    } else {
		object.put("result", "-1");
		object.put("errorMsg", "未配置默认文字语音信息通道！");
		object.put("describe", "未配置线路");
		object.put("code", "0010");
	    }
	} else {
	    // object.put("sessionId", null);
	    object.put("result", "-1");
	    object.put("errorMsg", "未找到相应的能力配置！");
	    object.put("describe", "能力配置错误");
	    object.put("code", "0013");
	}
	return object;
    }

    /**
     * 语音验证码实际业务处理
     * 
     * @param appExtendBean
     *            应用信息
     * @param accountBean
     *            账号信息
     * @param userIdentify
     *            用户标识
     * @param called
     *            被叫号码
     * @param chargeNbr
     *            IMS发起账号
     * @param key
     *            IMS发起密码
     * @param displayNumber
     *            显号
     * @param content
     *            语音验证码
     * @param voice_type
     *            语音留言类型
     * @return
     */
    private JSONObject sendVoiceCaptcha(AppExtendBean appExtendBean, AccountBean accountBean, String userIdentify, String called, String chargeNbr, String key,
	    String displayNumber, String content, String voice_type) {
	// 最终返回结果
	JSONObject object = new JSONObject();

	ResponseSendVoiceBean responseSendVoice = null;
	// 会话标识
	String session_id = "";
	try {
	    // 发起语音验证码
	    responseSendVoice = ImsVoiceNoticeUtils.sendIMSVoiceCaptcha(chargeNbr, key, displayNumber, called, content);
	} catch (Exception e1) {
	    e1.printStackTrace();
	}

	if (!"0".equals(responseSendVoice.getResult())) {
	    object.put("result", "-1");
	    object.put("errorMsg", "创建IMS语音验证码失败");
	    object.put("describe", "语音验证码异常");
	    object.put("code", "0601");
	} else {
	    object.put("result", "0");
	    object.put("describe", "IMS语音验证码存储成功");
	    object.put("code", "0000");
	    // 获取会话标识
	    session_id = responseSendVoice.getSessionid();
	    // 将sessionId写入Json串
	    object.put("session_id", session_id);

	    // 存储会议信息
	    // 拼写会议信息bean
	    MeetInfoBean meetInfoBean = new MeetInfoBean();
	    meetInfoBean.setAbility_type(voice_type);
	    meetInfoBean.setApp_key(appExtendBean.getApp_key());
	    meetInfoBean.setCalled(called);
	    meetInfoBean.setCaller(called);
	    meetInfoBean.setChargeNumber(chargeNbr);
	    meetInfoBean.setComments("");
	    meetInfoBean.setCust_account(accountBean.getCust_account());
	    meetInfoBean.setDuration(0);
	    meetInfoBean.setPeriphery_code(appExtendBean.getPeriphery_code());
	    meetInfoBean.setStatus("0");
	    meetInfoBean.setTotal_amount(2);
	    meetInfoBean.setUser_identify(userIdentify);
	    meetInfoBean.setSession_id(session_id);

	    // 存储相应信息
	    IMeetInfoService meetImsService = new MeetInfoServiceImpl();
	    try {
		meetImsService.insert(coreDao, meetInfoBean);
	    } catch (Throwable e) {
		Log.info("记录语音验证码信息失败！" + e);
	    }
	}

	return object;
    }
}
